#include<iostream>
#include<vector>
#include<unistd.h>
#include "Thread.hpp"
#include<functional>
#include<pthread.h>
#include "BlockQueue.hpp"

using namespace Thread_Module;



void* consumer(BlockQueue<int> &bq)
{
    while(true)
    {
        int data = 0;
        bq.Pop(&data);
        std::cout<<"消费一个数据:"<<data<<std::endl;
        sleep(1);
    }  
}

void* productor(BlockQueue<int> &bq)
{   
    int a = 1;
    while(true)
    {
        bq.Enqueue(a);
        std::cout<<"生产一个数据 :"<<a++<<std::endl;
    }
}

void Comm(std::vector<Thread<BlockQueue<int>>> *threads,int num,BlockQueue<int> &bq,func_t<BlockQueue<int>> func)
{
    for(int i=0;i<num;i++)
    {
        std::string name = "thread-"+std::to_string(i+1);
        threads->emplace_back(func,bq,name);
        threads->back().Start();
    }
}

void ProductorStart(std::vector<Thread<BlockQueue<int>>> *threads,int num,BlockQueue<int> &bq)
{
    Comm(threads,num,bq,productor);
}

void ConsumerStart(std::vector<Thread<BlockQueue<int>>> *threads,int num,BlockQueue<int> &bq)
{
    Comm(threads,num,bq,consumer);
}


void WaitAllThread(std::vector<Thread<BlockQueue<int>>> &threads)
{
    for(auto &thread:threads)
    {
        thread.Join();
    }
}
int main()
{
    BlockQueue<int> *bq = new BlockQueue<int>(5);

    std::vector<Thread<BlockQueue<int>>> threads;

    ProductorStart(&threads,1,*bq);
    ConsumerStart(&threads,1,*bq);
    WaitAllThread(threads);
    return 0;
}